package com.yr.ryjk.sms;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;

import com.yr.ryjk.common.Constant;
/**
 * 中国网建短信平台,短信发送的接口
 * @author huangxiaojun
 *
 */
public class SendSmsUtil {
	private static Logger log = Logger.getLogger(SendSmsUtil.class);
	
	public static boolean sendSms(String mobile,String  content) {
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://utf8.sms.webchinese.cn");
		post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf8");
		NameValuePair[] data = { 
				new NameValuePair("Uid", Constant.uid),
				new NameValuePair("Key", Constant.ukey),
				new NameValuePair("smsMob", mobile),
				new NameValuePair("smsText", content) 
		};
		post.setRequestBody(data);
		String result = "";
		Boolean flag = false;
		try {
			client.executeMethod(post);
			int statusCode = post.getStatusCode();
			if(statusCode==200){
				 log.info("短信发送状态("+statusCode+")：已发送");
			}
			result = new String(post.getResponseBodyAsString().getBytes("utf8"));
            if(result.equals("1")){
            	 log.info("验证码发送成功("+result+")");
            	flag = true;
            }else if(result.equals("-1")){
            	 log.info("没有该用户账户("+result+")");
            }else if(result.equals("-2")){
            	 log.info("密钥不正确("+result+")");
            }else if(result.equals("-21")){
            	 log.info("MD5接口密钥加密不正确("+result+")");
            }else if(result.equals("-3")){
            	 log.info("短信数量不足("+result+")");
            }else if(result.equals("-11")){
            	 log.info("该用户被禁用("+result+")");
            }else if(result.equals("-14")){
            	 log.info("短信内容出现非法字符("+result+")");
            }else if(result.equals("-4")){
            	 log.info("手机号格式不正确("+result+")");
            }else if(result.equals("-41")){
            	 log.info("手机号码为空("+result+")");
            }else if(result.equals("-42")){
            	 log.info("短信内容为空("+result+")");
            }else if(result.equals("-51")){
            	 log.info("短信签名格式不正确接口签名格式为：【签名内容】("+result+")");
            }else if(result.equals("-6")){
            	 log.info("IP限制("+result+")");
            }else{
            	log.info("短信发送数量("+result+")");
            }
			post.releaseConnection();
			return flag;
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
		return flag;
	}
	public static void main(String[] args) {
		Boolean msg = sendSms("13667101029", "验证码：8888 瑞翼健康");
		System.out.println(msg);
	}
}
